package com.oazon.moco.lan.util;

import io.netty.channel.ChannelHandlerContext;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.oazon.common.Logger;
import com.oazon.moco.lan.core.http.HttpJsonResponse;
import com.oazon.moco.lan.core.http.bean.JsonResponse;

public class LanServer {

	private static final String TAG = "LanServer";

	/**
	 * 响应回复
	 * 
	 * @param type
	 * @param code
	 * @param data 
	 * @param ctx 
	 */
	public static void responseResult(String type, int code, Object data,
			ChannelHandlerContext ctx) {

		JsonResponse message = new JsonResponse();
		message.setType(type);
		message.setData(data);
		message.setCode(code);
		if(data != null){
			data = JSON.toJSONString(data);
		}
		Logger.i(TAG, "code==" + code//
				+ "\r\ntype==" + type//
				+ "\r\ndata==" + data);

		if (ctx != null && ctx.channel() != null && ctx.channel().isActive()) {
			ctx.writeAndFlush(new HttpJsonResponse(null, message));
		}
	}
}
